﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace QuanLyThietBi
{
    class CXuly_thietbi
    {
        SqlConnection con;
        public CXuly_thietbi(SqlConnection con)
        {
            this.con = con;
        }
        public List<CThietbi> getdsthietbi()
        {
            con.Open();
            string str = "select * from thietbi where sophong is NULL";
            SqlCommand com = new SqlCommand(str, con);
            SqlDataReader data = com.ExecuteReader();
            List<CThietbi> list = new List<CThietbi>();
            while (data.Read())
            {
                CThietbi tb = new CThietbi();
                tb.Mathietbi = data["mathietbi"].ToString();
                tb.Tinhtrang = data["tinhtrang"].ToString();
                tb.Phieunhap.Maphieunhap = data["maphieunhap"].ToString();
                tb.Vattu.Mavattu = data["mavattu"].ToString();
                list.Add(tb);
            }
            con.Close();
            return list;
        }
        public List<CThietbi> getdsthietbi(string sophong)
        {
            con.Open();
            string str = "select * from thietbi where sophong=@sophong";
            SqlCommand com = new SqlCommand(str, con);
            com.Parameters.AddWithValue("@sophong", sophong);
            SqlDataReader data = com.ExecuteReader();
            List<CThietbi> list = new List<CThietbi>();
            while (data.Read())
            {
                CThietbi tb = new CThietbi();
                tb.Mathietbi = data["mathietbi"].ToString();
                tb.Tinhtrang = data["tinhtrang"].ToString();
                tb.Phieunhap.Maphieunhap = data["maphieunhap"].ToString();
                tb.Vattu.Mavattu = data["mavattu"].ToString();
                list.Add(tb);
            }
            con.Close();
            return list;
        }
        public List<string> getdsmathietbi()
        {
            List<string> ma = new List<string>();
            con.Open();
            string str = "select mathietbi from thietbi";
            SqlCommand com = new SqlCommand(str, con);
            SqlDataReader data = com.ExecuteReader();
            while (data.Read())
            {
                string a = data["mathietbi"].ToString();
                ma.Add(a);
            }
            con.Close();
            return ma;
        }
        public List<CThietbi> taomathietbi(CPhieunhap p)
        {
            List<CThietbi> dsthietbi = new List<CThietbi>();
            foreach (CChitietphieunhap a in p.Chitietphieunhap)
            {
                int soluong = a.Soluong;
                for (int i = 1; i <= soluong; i++)
                {
                    List<string> ds = getdsmathietbi();
                    string mathietbi = "";
                    string mavattu = a.Mavattu.ToString().Trim();
                    do
                    {
                        Random r = new Random();
                        int random = r.Next(1000, 9999);

                        mathietbi = "TB" + mavattu + random.ToString();
                    } while (ds.Contains(mathietbi));
                    CThietbi tb = new CThietbi();
                    tb.Mathietbi = mathietbi;
                    tb.Phieunhap = p;
                    tb.Vattu = a.Vattu;
                    dsthietbi.Add(tb);
                    con.Open();
                    string str = "insert into thietbi values(@mathietbi,@tinhtrang,@maphieunhap,@mavattu,@sophong)";
                    SqlCommand com = new SqlCommand(str, con);
                    com.Parameters.AddWithValue("@mathietbi", tb.Mathietbi);
                    com.Parameters.AddWithValue("@tinhtrang", "Tot");
                    com.Parameters.AddWithValue("@maphieunhap", tb.Phieunhap.Maphieunhap);
                    com.Parameters.AddWithValue("@mavattu", tb.Mavattu);
                    com.Parameters.AddWithValue("@sophong", System.DBNull.Value);
                    com.ExecuteNonQuery();
                    con.Close();

                }
            }
            //thaydoi_trangthaiphieunhap(p.Maphieunhap);
            con.Open();
            string s = "update phieunhap set trangthai=@trangthai where maphieunhap=@maphieunhap";
            SqlCommand c = new SqlCommand(s, con);
            c.Parameters.AddWithValue("@trangthai", 1);
            c.Parameters.AddWithValue("@maphieunhap", p.Maphieunhap);
            c.ExecuteNonQuery();
            con.Close();
            return dsthietbi;
        }
        public CThietbi tim(string mathietbi)
        {
            CThietbi tb = new CThietbi();
            con.Open();
            string str = "select * from thietbi where mathietbi=@mathietbi";
            SqlCommand com = new SqlCommand(str, con);
            com.Parameters.AddWithValue("@mathietbi", mathietbi);
            SqlDataReader data = com.ExecuteReader();
            while (data.Read())
            {
                tb.Mathietbi = data["mathietbi"].ToString();
                tb.Sophong = data["sophong"].ToString();
                tb.Tinhtrang = data["tinhtrang"].ToString();
                tb.Vattu.Mavattu = data["mavattu"].ToString();
                tb.Phieunhap.Maphieunhap = data["maphieunhap"].ToString();
            }
            con.Close();
            return tb;
        }
       
    }
}
